Flutter / UI Elements / App Bar
App Bar
-
Usage
Syntax
AppBar( title: /*some title*/, actions: [ //array of widgets that take some actions ], ), import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); /// main application widget class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); static const String _title = 'Flutter Tutorial'; @override Widget build(BuildContext context) { return const MaterialApp( title: _title, home: MyStatelessWidget(), ); } } /// stateless widget that the main application instantiates class MyStatelessWidget extends StatelessWidget { const MyStatelessWidget({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('AppBar Tutorial'), actions: [ IconButton( icon: const Icon(Icons.add_alert), onPressed: () { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('You pressed bell icon.'))); }, ), ], ), body: const Center( child: Text( 'Hello World!', style: TextStyle(fontSize: 24), ), ), ); } } IconButton in appBar() 1. Center aligned app title
centerTitle: true, 2. App bar bg color
backgroundColor: Colors.red,
-
Properties
1. title
Widget to display as the title of the app bar.
2. leadingWidget to display before the title. Typically used for a leading icon or button.
3. actionsList of widgets to display at the end of the app bar. These are usually icons or buttons for actions.
4. backgroundColorBackground color of the app bar.
5. elevationElevation (shadow) of the app bar.
6. brightnessBrightness of the app bar. It can be set to Brightness.light or Brightness.dark.
7. centerTitleWhether the title should be centered or aligned to the start.
8. automaticallyImplyLeadingControls whether the leading widget should automatically imply back navigation (defaults to true).
AppBar( title: Text('My App'), leading: IconButton( icon: Icon(Icons.menu), onPressed: () { // Add onPressed action for the leading icon }, ), actions: [ IconButton( icon: Icon(Icons.search), onPressed: () { // Add onPressed action for the search icon }, ), IconButton( icon: Icon(Icons.settings), onPressed: () { // Add onPressed action for the settings icon }, ), ], backgroundColor: Colors.blue, elevation: 4, // Adjust elevation as needed brightness: Brightness.dark, centerTitle: true, );